package com.joker.orange.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.joker.common.core.page.TableDataInfo;
import com.joker.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.joker.common.helper.LoginHelper;
import com.joker.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.joker.orange.domain.bo.CommentBo;
import com.joker.orange.domain.vo.CommentVo;
import com.joker.orange.domain.Comment;
import com.joker.orange.mapper.CommentMapper;
import com.joker.orange.service.ICommentService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 评论信息Service业务层处理
 *
 * @author Joker
 * @date 2024-06-09
 */
@RequiredArgsConstructor
@Service
public class CommentServiceImpl implements ICommentService {

    private final CommentMapper baseMapper;

    /**
     * 查询评论信息
     */
    @Override
    public CommentVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询评论信息列表
     */
    @Override
    public TableDataInfo<CommentVo> queryPageList(CommentBo bo, PageQuery pageQuery) {
        Comment comment = BeanUtil.toBean(bo, Comment.class);
        Page<CommentVo> result = baseMapper.selectPageCommentList(pageQuery.build(), this.buildQueryWrapper(comment));
        return TableDataInfo.build(result);
    }

    /**
     * 查询评论信息列表
     */
    @Override
    public List<CommentVo> queryList(CommentBo bo) {
        Comment comment = BeanUtil.toBean(bo, Comment.class);
        return baseMapper.selectVoList(this.buildQueryWrapper(comment));
    }

    private Wrapper<Comment> buildQueryWrapper(Comment bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<Comment> lqw = Wrappers.query();
        lqw.eq(bo.getUserId() != null, "c.user_id", bo.getUserId());
        lqw.like(ObjectUtil.isNotNull(bo.getCommentId()), "c.comment_id", bo.getCommentId());
        lqw.eq(StringUtils.isNotBlank(bo.getCommentType()), "c.comment_type", bo.getCommentType());
        lqw.eq(StringUtils.isNotBlank(bo.getAuditStatus()), "c.audit_status", bo.getAuditStatus());
        lqw.orderByDesc("c.create_time");
        return lqw;
    }

    /**
     * 新增评论信息
     */
    @Override
    public Boolean insertByBo(CommentBo bo) {
        Comment add = BeanUtil.toBean(bo, Comment.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改评论信息
     */
    @Override
    public Boolean updateByBo(CommentBo bo) {
        Comment update = BeanUtil.toBean(bo, Comment.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Comment entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除评论信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
